package com.lzh.persevere.controller;

import com.lzh.persevere.annotation.LogAnnotation;
import com.lzh.persevere.common.LogType;
import com.lzh.persevere.common.R;
import com.lzh.persevere.common.ResultUtil;
import com.lzh.persevere.model.dto.user.LoginUserDto;
import com.lzh.persevere.model.dto.user.UserRegisterDto;
import com.lzh.persevere.model.entity.User;
import com.lzh.persevere.model.entity.UserRole;
import com.lzh.persevere.model.qo.IdQO;
import com.lzh.persevere.model.qo.user.UserPageQO;
import com.lzh.persevere.model.ro.IdRO;
import com.lzh.persevere.model.ro.UrlRO;
import com.lzh.persevere.model.ro.user.AddUserRO;
import com.lzh.persevere.model.ro.user.RegisterEmailRO;
import com.lzh.persevere.model.ro.user.UpdatePasswordRO;
import com.lzh.persevere.model.ro.user.UpdateUserRO;
import com.lzh.persevere.model.vo.user.LoginUserVo;
import com.lzh.persevere.service.UserRoleService;
import com.lzh.persevere.service.UserService;
import com.lzh.persevere.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author luzhiheng
 * @description 用户控制器
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @ApiOperation(value = "分页查询用户列表")
    @PostMapping("/findUserPageList")
    public R<Map<String, Object>> findUserPageList(@Valid @RequestBody UserPageQO qo) {
        return ResultUtil.success(userService.pageList(qo));
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody LoginUserDto dto) {
        return userService.login(dto);
    }

    @ApiOperation(value = "发送注册验证码 - 邮件")
    @PostMapping("/sendEmailRegisterCode")
    public R<Boolean> sendEmailRegisterCode(@Valid @RequestBody RegisterEmailRO ro) {
        return ResultUtil.success(userService.sendEmailRegisterCode(ro));
    }

    @ApiOperation(value = "完成用户注册")
    @PostMapping("/finishRegister")
    public R<Boolean> finishRegister(@Valid @RequestBody UserRegisterDto dto) {
        return ResultUtil.success(userService.finishRegister(dto));
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public R logout() {
        return userService.logout();
    }

    @ApiOperation(value = "查询用户详情")
    @PostMapping("/findUserDetail")
    public R<User> findUserDetail(@Valid @RequestBody IdQO qo) {
        return ResultUtil.success(userService.getUserById(qo.getId()));
    }

    @ApiOperation(value = "获取登录用户信息")
    @GetMapping("/get/login")
    public R<LoginUserVo> getLoginUser() {
        LoginUserVo user = userService.getLoginUser();
        return ResultUtil.success(user);
    }

    @LogAnnotation(message = "更新用户头像", operation = LogType.UPDATE)
    @PostMapping("/updateAvatar")
    public R<Boolean> updateUserAvatar(@RequestBody UrlRO ro) {
        return ResultUtil.success(userService.updateUserAvatar(ro.getUrl()));
    }

    @LogAnnotation(message = "更新用户信息", operation = LogType.UPDATE)
    @PutMapping("/updateUserInfo")
    public R<Boolean> updateUserInfo(@RequestBody UpdateUserRO ro) {
        User user = new User();
        user.setId(SecurityUtils.getUserId());
        user.setNickName(ro.getNickName());
        user.setSex(ro.getSex());
        user.setRemark(ro.getRemark());
        return ResultUtil.success(userService.updateById(user));
    }

    @Transactional(rollbackFor = Exception.class)
    @LogAnnotation(message = "编辑用户信息", operation = LogType.UPDATE)
    @PutMapping("/editUserInfo")
    public R<Boolean> editUserInfo(@Valid @RequestBody UpdateUserRO ro) {
        User user = new User();
        user.setId(ro.getId());
        user.setNickName(ro.getNickName());
        user.setSex(ro.getSex());
        user.setRemark(ro.getRemark());
        user.setEmail(ro.getEmail());
        user.setPhone(ro.getPhone());
        user.setAvatar(ro.getAvatar());
        user.setAddress(ro.getAddress());
        user.setStatus(ro.getStatus());

        userRoleService.removeByUserId(user.getId());
        UserRole userRole = new UserRole().setUserId(user.getId()).setRoleId(ro.getRoleId());
        userRoleService.save(userRole);
        return ResultUtil.success(userService.updateById(user));
    }

    @LogAnnotation(message = "更新密码", operation = LogType.UPDATE)
    @PutMapping("/updatePassword")
    public R<Boolean> updatePassword(@RequestBody UpdatePasswordRO ro) {
        return ResultUtil.success(userService.updateUserPassword(ro));
    }

//    @LogAnnotation(message = "重置密码", operation = LogType.UPDATE)
//    @PutMapping("/rePassword")
//    public ResponseResult rePassword(@RequestBody UpdatePwdDTO user) {
//        return userService.rePassword(user.getEmail(), user.getNewPassword(), user.getConPassword());
//    }

    @LogAnnotation(message = "注销账号", operation = LogType.DELETE)
    @DeleteMapping("/cancel/{userId}")
    public R<Object> cancelAccount(@PathVariable Long userId) {
        return userService.cancelAccount(userId);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/addUser")
    public R<Boolean> addUser(@Valid @RequestBody AddUserRO ro) {
        return ResultUtil.success(userService.addUser(ro));
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/deleteUser")
    public R<Boolean> deleteUser(@Valid @RequestBody IdRO ro) {
        return ResultUtil.success(userService.removeById(ro.getId()));
    }

}
